<template>
  <div class="chat-input">
    <div class="input-container">
      <textarea
        ref="messageInput"
        v-model="message"
        :placeholder="placeholder"
        :disabled="disabled"
        @keydown="handleKeydown"
        @input="adjustHeight"
        rows="1"
        class="message-textarea"
      ></textarea>
      <button
        :disabled="!canSend"
        @click="sendMessage"
        class="send-button"
      >
        <span v-if="!disabled">发送</span>
        <div v-else class="loading"></div>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatInput',
  props: {
    placeholder: {
      type: String,
      default: '请输入您的消息...'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      message: ''
    }
  },
  computed: {
    canSend() {
      return this.message.trim() && !this.disabled
    }
  },
  methods: {
    sendMessage() {
      if (this.canSend) {
        this.$emit('send', this.message.trim())
        this.message = ''
        this.adjustHeight()
      }
    },
    handleKeydown(event) {
      if (event.key === 'Enter' && !event.shiftKey) {
        event.preventDefault()
        this.sendMessage()
      }
    },
    adjustHeight() {
      this.$nextTick(() => {
        const textarea = this.$refs.messageInput
        textarea.style.height = 'auto'
        textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px'
      })
    },
    focus() {
      this.$refs.messageInput.focus()
    }
  },
  mounted() {
    this.focus()
  }
}
</script>

<style scoped>
.chat-input {
  padding: 20px;
  background: white;
  border-top: 1px solid #e9ecef;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  max-width: 1000px;
  margin: 0 auto;
}

.message-textarea {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 20px;
  padding: 12px 16px;
  font-size: 16px;
  line-height: 1.5;
  resize: none;
  outline: none;
  transition: border-color 0.3s ease;
  font-family: inherit;
  min-height: 24px;
  max-height: 120px;
  overflow-y: auto;
}

.message-textarea:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.message-textarea:disabled {
  background-color: #f8f9fa;
  cursor: not-allowed;
}

.send-button {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 50%;
  width: 48px;
  height: 48px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.send-button:hover:not(:disabled) {
  background: linear-gradient(45deg, #5a6fd8, #6a4190);
  transform: scale(1.05);
}

.send-button:disabled {
  background: #6c757d;
  cursor: not-allowed;
  transform: none;
}

.loading {
  width: 16px;
  height: 16px;
  border: 2px solid #ffffff40;
  border-top: 2px solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .chat-input {
    padding: 16px;
  }
  
  .input-container {
    gap: 8px;
  }
  
  .send-button {
    width: 40px;
    height: 40px;
    font-size: 14px;
  }
}
</style>